define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mental/question/index' + location.search,
                    add_url: 'mental/question/add'+ '?main_id='+Config.main_id,
                    edit_url: 'mental/question/edit',
                    del_url: 'mental/question/del',
                    multi_url: 'mental/question/multi',
                    import_url: 'mental/question/import'+ '?main_id='+Config.main_id,
                    table: 'mental_question',
                },
                // queryParams: function (params) { //自定义搜索条件
                //     var filter = params.filter ? JSON.parse(params.filter) : {}; //判断当前是否还有其他高级搜索栏的条件
                //     var op = params.op ? JSON.parse(params.op) : {};  //并将搜索过滤器 转为对象方便我们追加条件
                //     filter.main_id= Config.main_id;     //将透传的参数 Config.group_id，追加到搜索条件中
                //     op.main_id= "=";  //group_id的操作方法的为 找到相等的
                //     params.filter = JSON.stringify(filter); //将搜索过滤器和操作方法 都转为JSON字符串
                //     params.op = JSON.stringify(op);
                //     //如果希望忽略搜索栏搜索条件,可使用
                //     //params.filter = JSON.stringify({url: 'login'});
                //     //params.op = JSON.stringify({url: 'like'});
                //     // console.log(Config);
                //     return params;

                // },
            });
            Table.button.edit.extend = Table.button.edit.extend + ` data-area='["80%","80%"]'`
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                // pk: 'main_id',
                // sortName: 'main_id',
                queryParams:function(params){
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    var main_id = Fast.api.query('ids');  
                  
                    if (main_id) {
                          filter.main_id = main_id;
                          op.main_id = '=';
                          params.filter = JSON.stringify(filter);
                          params.op = JSON.stringify(op);
                        //   funcUrlDel('ids');
                    }
                    return params;
                  
                },
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'type', title: __('Type'), searchList: { "skip": __('Type skip'), "mark": __('Type mark') }, formatter: Table.api.formatter.normal },
                        { field: 'title', title: __('Title') },
                        // { field: 'name', title: __('Category_id'), operate: false },
                        // { field: 'category_id', title: __('Category_id'), visible: false, searchList: $.getJSON("mental/category/getList") },
                        { field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        // {field: 'answer', title: __('Answer')},
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'updatetime', title: __('Updatetime'), operate: false, addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        select: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mental/question/index' + location.search,
                    add_url: '',
                    edit_url: '',
                    del_url: '',
                    multi_url: '',
                    table: 'mental_select',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'type', title: __('Type'), searchList: { "skip": __('Type skip'), "mark": __('Type mark') }, formatter: Table.api.formatter.normal },
                        { field: 'title', title: __('Title') },
                        { field: 'name', title: __('Category_id') },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },

                    ]
                ]
            });

            $(document).on('click', '.btn-callback', function () {
                Fast.api.close(table.bootstrapTable('getSelections'));
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'mental/question/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'title', title: __('Title'), align: 'left' },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'mental/question/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'mental/question/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(document).on("click", ".btn-append", function(){
				Form.events.plupload("#add-form");
			});
            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on("click", ".btn-append", function(){
				Form.events.plupload("#edit-form");
			});
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});